package com.young.netty.gateway.gateway;

import com.young.netty.gateway.config.NettyGatewayConfig;
import com.young.netty.gateway.interceptor.InterceptorChain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

/**
 * NettyGateway启动类
 *
 * @author Young
 * @Date 2021-07-24 20:00
 */
@Component
public class NettyGatewayBootstrap implements ApplicationListener<ApplicationStartedEvent> {

    private NettyGateway nettyGateway;

    @Autowired
    private NettyGatewayConfig nettyGatewayConfig;

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        // 1. 加载拦截器
        InterceptorChain interceptorChain = new InterceptorChain();
        // 2. 启动网关服务
        NettyGateway nettyGateway = new NettyGateway(interceptorChain, nettyGatewayConfig);
        nettyGateway.start();
        this.nettyGateway = nettyGateway;
    }


    @PreDestroy
    public void destroy() {
        nettyGateway.shoudown();
    }
}
